/*
 * CUMediaWriter.h
 *
 *  Created on: 2020年7月18日
 *      Author: terry
 */

#ifndef CUMEDIAWRITER_H_
#define CUMEDIAWRITER_H_

#include "UMediaWriter.h"
#include "Ffmpeg.h"
#include <mutex>


class CUMediaWriter: public UMediaWriter
{
public:
	explicit CUMediaWriter(int fileFormat = 0);
	virtual ~CUMediaWriter();

	virtual bool open(const char* filename, const UMediaFormat& fmt);

	virtual void close();

	virtual bool isOpen();

	virtual bool getFormat(UMediaFormat& fmt);

    virtual bool write(UMediaPacket& pkt);

    /**
     * 设置输出文件路径. 可用于切换录像
     * @param filepath	文件路径
     * @return true 表示成功
     */
    virtual bool setFile(const char* filepath);

    virtual int getFileFormat();


protected:

    bool openFile();
    void closeFile();
    bool isFileOpen();

    void writeVideo(AVPacket& inPkt);
    void writeAudio(AVPacket& inPkt);
    void doWriteAudio(AVPacket* pkt, int rate);
	
	void writeSubtitle(AVPacket& inPkt);

	bool checkAudioSupport(int fileFormat, AVFormatContext* fmtCtx, AVCodecID codecID);

protected:
    std::mutex  m_mutex;
    int     m_fileFormat;

    UMediaFormat m_format;

    std::string m_fileName;
    bool    m_changeFile;

    AVFormatContext*    m_fmtCtx;

    int m_videoIndex;
    int m_audioIndex;
	int m_subtitleIndex;

    int64_t m_videoStartTime;
    int64_t m_videoCurrentTime;
    int64_t m_audioStartTime;
	int64_t m_subtitleStartTime;
    int64_t m_audioCurrentTime;

	bool	m_opened;

};

#endif /* CUMEDIAWRITER_H_ */
